package org.argetr.resim.proc.view;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;

import java.awt.*;
import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;

import javax.swing.*;

import org.argetr.resim.proc.ConnectionRule;
import org.argetr.resim.proc.data.Data;
import org.argetr.resim.proc.data.ImageDA;
import org.argetr.resim.ui.viewers.ImageView;
import org.argetr.resim.ui.viewers.ViewCounter;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;



import com.sun.media.jai.widget.DisplayJAI;



public class ImgViewerPR extends ViewerPR {
	protected void setConnectionRules(ConnectionRule connRule){
		connRule.addInType(ImageDA.class, 0);
	}
	public Data process() {
		PlanarImage k = getInputAsPlanarImage();
		JFrame frame = new JFrame();
		frame.setTitle("ImageViewer");
		Container contentPane = frame.getContentPane();
		contentPane.setLayout(new BorderLayout());
		ParameterBlock pbConvert = new ParameterBlock();
		pbConvert.addSource(k);
		pbConvert.add(DataBuffer.TYPE_BYTE);
		PlanarImage i = JAI.create("format", pbConvert);

	    IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
	    if (workbenchWindow == null) {
		  return null;
	    }
		
	    IWorkbenchPage workbenchPage = workbenchWindow.getActivePage();
	    if (workbenchPage == null) {
	      return null;
	    }
		 
	    try {
		  ImageView viewPart = (ImageView)workbenchPage.showView(ImageView.ID,new Integer(ViewCounter.VIEW_INSTANCE_COUNT++).toString(),IWorkbenchPage.VIEW_ACTIVATE);
		  viewPart.setViewerTitle("Image View");
		  viewPart.setPanel(new DisplayJAI(i));
	    } 
	    catch (PartInitException e) {		  
			e.printStackTrace();
	    }		
		return null;
	}

}